import { getCartProductList, updateUserAddress } from '@/api/shop'

const shop = {
  state: {
    cartProductData: null,
    addressId: '',
    cartNum: 0
  },
  mutations: {
    SET_CARTPRODUCT_DATA(state, cartProductData) {
      state.cartProductData = cartProductData
    },
    SET_ADDRESS_ID(state, data) {
      state.addressId = data
    },
    SET_CART_NUM(state, data) {
      state.cartNum = data
    }
  },
  actions: {
    // 获取购物车信息
    getCartProductList({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        getCartProductList(parameterData)
          .then(response => {
            commit('SET_CARTPRODUCT_DATA', response)
            commit('SET_CART_NUM', response.data.cartNum)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    },

    // 添加/更新地址
    UpdateUserAddress({ commit, state }, parameterData) {
      return new Promise((resolve, reject) => {
        updateUserAddress(parameterData)
          .then(response => {
            commit('SET_ADDRESS_ID', response.data.addressId)
            resolve(response)
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}

export default shop
